package com.espier.t9keyboard;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.widget.Toast;


public class ContactsDBUtils {
    private static final String SIM_CONTENT_URI = "content://icc/adn";
    
    private static String[] clumns = {Contacts._ID,                           // 0
            Contacts.DISPLAY_NAME,       
            };
    
    public static class ContactQuery {
//        private static final String[] CONTACT_PROJECTION_PRIMARY = new String[] {
//            Contacts._ID,                           // 0
//            Contacts.DISPLAY_NAME_PRIMARY,          // 1
//            Contacts.CONTACT_PRESENCE,              // 2
//            Contacts.CONTACT_STATUS,                // 3
//            Contacts.PHOTO_ID,                      // 4
//            Contacts.PHOTO_THUMBNAIL_URI,           // 5
//            Contacts.LOOKUP_KEY,                    // 6
//            Contacts.IS_USER_PROFILE,               // 7
//        };
        
        private static final String[] CONTACT_PROJECTION_PRIMARY = new String[] {
            Contacts._ID,                           // 0
            Contacts.DISPLAY_NAME,          // 1
            Contacts.CONTACT_PRESENCE,              // 2
            Contacts.CONTACT_STATUS,                // 3
            Contacts.PHOTO_ID,                      // 4
            Contacts.Photo.CONTENT_DIRECTORY,           // 5
            Contacts.LOOKUP_KEY,                    // 6
        //    Contacts.,               // 7
        };


//        private static final String[] CONTACT_PROJECTION_ALTERNATIVE = new String[] {
//            Contacts._ID,                           // 0
//            Contacts.DISPLAY_NAME_ALTERNATIVE,      // 1
//            Contacts.CONTACT_PRESENCE,              // 2
//            Contacts.CONTACT_STATUS,                // 3
//            Contacts.PHOTO_ID,                      // 4
//            Contacts.PHOTO_THUMBNAIL_URI,           // 5
//            Contacts.LOOKUP_KEY,                    // 6
//            Contacts.IS_USER_PROFILE,               // 7
//        };
//
//        private static final String[] FILTER_PROJECTION_PRIMARY = new String[] {
//            Contacts._ID,                           // 0
//            Contacts.DISPLAY_NAME_PRIMARY,          // 1
//            Contacts.CONTACT_PRESENCE,              // 2
//            Contacts.CONTACT_STATUS,                // 3
//            Contacts.PHOTO_ID,                      // 4
//            Contacts.PHOTO_THUMBNAIL_URI,           // 5
//            Contacts.LOOKUP_KEY,                    // 6
//            Contacts.IS_USER_PROFILE,               // 7
//           // SearchSnippetColumns.SNIPPET,           // 8
//        };
//
//        private static final String[] FILTER_PROJECTION_ALTERNATIVE = new String[] {
//            Contacts._ID,                           // 0
//            Contacts.DISPLAY_NAME_ALTERNATIVE,      // 1
//            Contacts.CONTACT_PRESENCE,              // 2
//            Contacts.CONTACT_STATUS,                // 3
//            Contacts.PHOTO_ID,                      // 4
//            Contacts.PHOTO_THUMBNAIL_URI,           // 5
//            Contacts.LOOKUP_KEY,                    // 6
//            Contacts.IS_USER_PROFILE,               // 7
//           // SearchSnippetColumns.SNIPPET,           // 8
//        };

        public static final int CONTACT_ID               = 0;
        public static final int CONTACT_DISPLAY_NAME     = 1;
        public static final int CONTACT_PRESENCE_STATUS  = 2;
        public static final int CONTACT_CONTACT_STATUS   = 3;
        public static final int CONTACT_PHOTO_ID         = 4;
        public static final int CONTACT_PHOTO_URI        = 5;
        public static final int CONTACT_LOOKUP_KEY       = 6;
        public static final int CONTACT_IS_USER_PROFILE  = 7;
        public static final int CONTACT_SNIPPET          = 8;
    }
    
    public static  Cursor queryContacts(Context context,ContentResolver mContentResolver){
        Cursor c = mContentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
       return c;
    }
    
    public static Cursor getSimContacts(Context context,ContentResolver mContentResolver){
        Intent intent = new Intent();
        intent.setData(Uri.parse(SIM_CONTENT_URI));
        Uri uri = intent.getData();
        Cursor cursor = mContentResolver.query(uri, null, null, null, null);
        if(cursor == null){
            Toast.makeText(context, "cursor is null", Toast.LENGTH_LONG).show();
            return null;
        }
        return cursor;
    }
    

     
     public static String getPhoneText(List<ContactPhone> contacList){
         String number = contacList.get(0).getNumber();
         for(int i = 0;i < contacList.size();i++ ){
             ContactPhone phone = contacList.get(i);
             if(phone.getIsPrimary().equals("1")){
                 number =  phone.getNumber();
                 break;
             }
         }
         return number;
     }
     
}
